﻿using MslDiary.Utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using MFW = MslDiary.Framework;
using MslDiary.Framework;

// 版权所有：2014  All Rights Reserved.
// 文 件 名：MainWindowViewModel.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2014/12/6 15:25:27
// 修改描述：
//----------------------------------------------------------------*/
namespace MslDiary.ViewModel
{
    public class MainWindowViewModel : MslDiary.Framework.ViewModelBase
    {
        private DispatcherTimer timer;
        private System.Windows.Window win = null;
        private View.SkinList frmSkin = null;
        private View.ItemList frmItem = null;
        private View.Config frmConfig = null;
        private View.About frmAbout = null;
        private View.RemoveLock frmRemoveLock = null;
        private const string LOCK_IMAGE_PATH = "/Theme/Images/Menu/bar_lock.png";
        private const string LOCK_OPEN_IMAGE_PATH = "/Theme/Images/Menu/bar_lock_open.png";
        public MainWindowViewModel()
        {
            Loading();
            MslDiary.Utils.Helper.Instance.ShowPupUp("温馨提示", "欢迎使用本软件", "\r\n      时光之痕日记:本软件由m.sh.lin0328@163.com开发与维护，免费使用，如有好的意见或建议，可发送邮件到 m.sh.lin0328@163.com，谢谢您的使用！");
            this.WindowLoadCommand = new CommandBase((Object window) =>
            {
                if (window != null)
                {
                    this.win = window as System.Windows.Window;
                    Gloable.MainWindowWidth = this.win.Width;
                    MslDiary.Utils.Helper.Instance.SetWindowBackground(this.win, MslDiary.Framework.Gloable.AppBackgroundImage);
                }
            });
            this.MenuButtonCommand = new CommandBase((Object arg) =>
            {
                MenuButtonClick(arg == null ? "" : arg.ToString());
            });
            this.LockDiaryCommand = new CommandBase((Object arg) =>
            {
                if (!Gloable.DiaryIsLock)
                {
                    MslDiary.Utils.Helper.Instance.AlertWarning("您的日记本没有设定锁定功能！");
                    return;
                }
                LockOrUnLock();
            });
        }
        /// <summary>
        /// 解锁/锁定
        /// </summary>
        private void LockOrUnLock()
        {
            if (MFW.Gloable.DiaryIsLock && String.IsNullOrEmpty(MFW.Gloable.DiaryLockPwd))
            {
                MslDiary.Utils.Helper.Instance.AlertConfirm("提示", "请先设定锁定密码！", () =>
                {
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        //if (this.frmConfig == null)
                            this.frmConfig = new View.Config();
                        this.frmConfig.Owner = this.win;
                        this.frmConfig.Show();
                    }));
                });
                return;
            }
            if (MFW.Gloable.DiaryIsLock && !MFW.Gloable.IsImmediatelyLock)
            {
                MFW.Gloable.IsImmediatelyLock = true;
                return;
            }
            if (MFW.Gloable.IsImmediatelyLock)
            {
                //if (this.frmRemoveLock == null)
                    this.frmRemoveLock = new View.RemoveLock();
                this.frmRemoveLock.Owner = this.win;
                this.frmRemoveLock.Show();
            }
        }
        #region 属性

        private bool _showWindowMenu = false;
        private string _lockButtonMenuText = "立即锁定";
        private bool _isShowMaxButton = true;
        private bool _isShowRestoreButton = false;
        private string _lockImagePath = LOCK_IMAGE_PATH;
        /// <summary>
        /// 锁定菜单图片
        /// </summary>
        public string LockImagePath
        {
            get { return _lockImagePath; }
            set
            {
                //base.SetProperty<string>(ref this._lockImagePath, value);
                if (this._lockImagePath == value)
                    return;
                this._lockImagePath = value;
                this.RaisePropertyChanged("LockImagePath");
            }
        }
        /// <summary> 是否显示最大化按钮 </summary>
        public bool IsShowMaxButton
        {
            get { return _isShowMaxButton; }
            set
            {
                //base.SetProperty<bool>(ref this._isShowMaxButton, value);
                if (this._isShowMaxButton == value)
                    return;
                this._isShowMaxButton = value;
                this.RaisePropertyChanged("IsShowMaxButton");
            }
        }
        /// <summary> 是否显示还原按钮 </summary>
        public bool IsShowRestoreButton
        {
            get { return _isShowRestoreButton; }
            set
            {
                //base.SetProperty<bool>(ref this._isShowRestoreButton, value);
                if (this._isShowRestoreButton == value)
                    return;
                this._isShowRestoreButton = value;
                this.RaisePropertyChanged("IsShowRestoreButton");
            }
        }
        /// <summary>锁定按键</summary>
        public string LockButtonMenuText
        {
            get { return _lockButtonMenuText; }
            set
            {
                //base.SetProperty<string>(ref this._lockButtonMenuText, value);
                if (this._lockButtonMenuText == value)
                    return;
                this._lockButtonMenuText = value;
                this.RaisePropertyChanged("LockButtonMenuText");
            }
        }
        /// <summary> 程序版本 </summary>
        public string Verson { get; set; }
        /// <summary> 程序中文名 </summary>
        public string ProcessName { get; set; }
        /// <summary> 登录用户信息 </summary>
        public string UserInfo { get; set; }
        /// <summary> 是否显示窗体菜单 </summary>
        public bool ShowWindowMenu
        {
            get { return _showWindowMenu; }
            set
            {
                //base.SetProperty<bool>(ref this._showWindowMenu, value);
                if (this._showWindowMenu == value)
                    return;
                this._showWindowMenu = value;
                this.RaisePropertyChanged("ShowWindowMenu");
            }
        }
        #endregion

        #region 命令
        public ICommand WindowLoadCommand { get; set; }
        /// <summary>系统菜单</summary>
        public ICommand MenuButtonCommand { get; set; }
        /// <summary>立即锁定</summary>
        public ICommand LockDiaryCommand { get; set; }
        private ICommand _windowMaxCommand;
        private ICommand _windowMinCommand;
        private ICommand _windowCloseCommand;
        private ICommand _windowRestoreCommand;
        /// <summary>最大化</summary>
        public ICommand WindowMaxCommand
        {
            get
            {
                _windowMaxCommand = new CommandBase((Object p) =>
                {
                    if (this.win != null)
                    {
                        Rect rect = SystemParameters.WorkArea;
                        this.win.MaxWidth = rect.Width;// +10;
                        this.win.MaxHeight = rect.Height;// + 10;
                        this.win.WindowState = WindowState.Maximized;
                        this.IsShowMaxButton = false;
                        this.IsShowRestoreButton = true;

                        Gloable.MainWindowWidth = rect.Width;
                        MslDiary.Framework.Gloable.IsWidthChanged = true;
                    }
                });
                return _windowMaxCommand;
            }
        }
        /// <summary>还原</summary>
        public ICommand WindowRestoreCommand
        {
            get
            {
                _windowRestoreCommand = new CommandBase((Object p) =>
                {
                    WindowSwitchMax();
                });
                return _windowRestoreCommand;
            }
        }
        /// <summary>最小化</summary>
        public ICommand WindowMinCommand
        {
            get
            {
                if (_windowMinCommand == null)
                {
                    _windowMinCommand = new CommandBase((Object mod) =>
                    {
                        if (this.win != null)
                        {
                            this.win.WindowState = WindowState.Minimized;
                        }
                    });
                }
                return _windowMinCommand;
            }
        }
        /// <summary>关闭</summary>
        public ICommand WindowCloseCommand
        {
            get
            {
                if (_windowCloseCommand == null)
                {
                    _windowCloseCommand = new CommandBase((Object mod) =>
                    {
                        if (MslDiary.Framework.Gloable.MinimizeToTray)
                        {
                            this.win.ShowInTaskbar = false;
                            this.win.WindowState = System.Windows.WindowState.Minimized;
                            NotifyIconHelper.Instance().ShowBalloonTip("提示", "程序已经最小化到系统托盘！");
                            return;
                        }
                        Utils.Helper.Instance.ExitApp();
                    });
                }
                return _windowCloseCommand;
            }
        }
        #endregion

        #region 方法
        private void WindowSwitchMax()
        {
            if (this.win != null)
            {
                this.win.WindowState = WindowState.Normal;
                this.IsShowMaxButton = true;
                this.IsShowRestoreButton = false;

                MslDiary.Framework.Gloable.IsWidthChanged = true;
                Gloable.MainWindowWidth = this.win.Width;
            }
        }
        /// <summary>
        /// 任务栏单击击图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TaskBarLeftDown_Click(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (this.win != null)
                {
                    this.win.Show();
                    if (this.win.WindowState == System.Windows.WindowState.Minimized)
                    {
                        this.win.WindowState = System.Windows.WindowState.Normal;
                        this.win.ShowInTaskbar = true;
                    }
                    this.win.Activate();
                }
            }
        }
        private void MenuButtonClick(string arg)
        {
            if (String.IsNullOrEmpty(arg))
                return;

            switch (arg)
            {
                case "ITEM"://字典维护
                    //if (this.frmItem == null)
                    //{
                        this.frmItem = new View.ItemList();
                        this.frmItem.Owner = this.win;
                    //}
                    this.frmItem.Show();
                    break;
                case "CHK_UPDATE"://检查更新
                    MslDiary.Utils.Helper.Instance.Alert(MslDiary.Framework.ShowMessage.NEWFUN_DEVELOP_MSG01);
                    break;
                case "SET"://设置
                    //if (this.frmConfig == null)
                    //{
                        this.frmConfig = new View.Config();
                        this.frmConfig.Owner = this.win;
                    //}
                    this.frmConfig.Show();
                    break;
                case "ABOUT_US"://关于我们
                    //if (this.frmAbout == null)
                    //{
                        this.frmAbout = new View.About();
                        this.frmAbout.Owner = this.win;
                    //}
                    this.frmAbout.Show();
                    break;
                case "EXIT"://退出
                    MslDiary.Utils.Helper.Instance.ExitApp();
                    break;
                case "DIARY_LOCK"://锁定/解锁
                    LockOrUnLock();
                    break;
                case "SKIN"://打开换肤窗口
                    //if (this.frmSkin == null)
                    //{
                        this.frmSkin = new View.SkinList();
                        this.frmSkin.Owner = this.win;
                    //}
                    this.frmSkin.Show();
                    break;
            }
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Loading()
        {
            string verson = MslDiary.Utils.Helper.Instance.GetVersion();
            string name = "时光之痕日记";
            this.ProcessName = name + " V" + verson.Substring(0, verson.IndexOf(".") + 2);
            this.Verson = name + " 主程序版本 V" + MslDiary.Utils.Helper.Instance.GetVersion();
            this.UserInfo = String.Format("账号：{0}  昵称：{1}", MFW.Gloable.CurrentLoginedAccount, MFW.Gloable.NickName);

            this.timer = new DispatcherTimer();
            this.timer.Interval = TimeSpan.FromSeconds(1);//更新锁定按钮文本
            this.timer.Tick += Timer_Tick;
            this.timer.Start();
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (MslDiary.Framework.Gloable.IsSkinChanged || MslDiary.Framework.Gloable.IsWidthChanged)
            {
                MslDiary.Utils.Helper.Instance.SetWindowBackground(this.win, MslDiary.Framework.Gloable.AppBackgroundImage);
                MslDiary.Framework.Gloable.IsSkinChanged = false;
                MslDiary.Framework.Gloable.IsWidthChanged = false;
            }
            if (MslDiary.Framework.Gloable.IsImmediatelyLock)
            {
                this.LockImagePath = LOCK_IMAGE_PATH;
                this.LockButtonMenuText = "解除锁定";
            }
            else
            {
                this.LockImagePath = LOCK_OPEN_IMAGE_PATH;
                this.LockButtonMenuText = "立即锁定";
            }
        }

        #endregion
    }
}
